package com.example.xui;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.xui.Way.changStatusIconCollor;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.wildma.pictureselector.PictureBean;
import com.xuexiang.xui.XUI;
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner;

import org.angmarch.views.NiceSpinner;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class Post extends AppCompatActivity{
    List<String> list_qian=new ArrayList<>();
    private RichEditer richEditer;
    private NiceSpinner niceSpinner;
    private static PictureBean pictureBean;
    List<Uri> uriList=new ArrayList<Uri>();
    String label;
    String connect;
    EditText title;
    String data;
    String sno;
    FloatingActionButton floatingActionButton;
    //List<String> list_qian=new ArrayList<>();
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //    XUI.initTheme(this);
        setContentView(R.layout.activity_post);
        Window window =this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
        sno=getIntent().getStringExtra("sno");
        changStatusIconCollor changStatusIconCollor=new changStatusIconCollor();
        list_qian.add(" ");
        list_qian.add("计算机");
        list_qian.add("医学");
        list_qian.add("药学");
        list_qian.add("考研");
        list_qian.add("护理学");
        niceSpinner=findViewById(R.id.nice_spinner);
        niceSpinner.attachDataSource(list_qian);
        niceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    label=parent.getItemAtPosition(position-1).toString();
                    Log.i("label","tagg="+label);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        changStatusIconCollor.changStatusIconCollor1(window,true);
        init1();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                connect=richEditer.toHtml();
             //   richEditer.getContent();
                SimpleDateFormat formatter=new SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");
                Date curDate =  new Date(System.currentTimeMillis());
                data=formatter.format(curDate);
                Log.i("tag","tag="+title.getText().toString());
                Log.i("tag","tag="+sno);
                Log.i("tag","tag="+connect);
                Log.i("tag","tag="+label);
                Log.i("tag","tag="+data);
                add_Label(sno,title.getText().toString(),label,connect,data);
                finish();
            }
        });
    }

    public void init1(){
        richEditer=findViewById(R.id.editor_content);
        ImageView imageView=findViewById(R.id.imageView5);
        ImageView imageView1=findViewById(R.id.imageView6);
        ImageView imageView2=findViewById(R.id.imageView7);
        ImageView imageView3=findViewById(R.id.imageView8);
        ImageView imageView4=findViewById(R.id.imageView9);
        ImageView imageView5=findViewById(R.id.imageView11);
        ImageView imageView6=findViewById(R.id.imageView13);
        title=findViewById(R.id.editText);
        floatingActionButton=findViewById(R.id.add_note);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                richEditer.bold();
            }
        });
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                richEditer.italic();
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                richEditer.fontSize();
            }
        });
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                richEditer.alignment();
            }
        });
        imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                paizhao1(v);
            }
        });
    }

    public void paizhao1(View v){
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, 2);
        Toast.makeText(Post.this,richEditer.toHtml(),Toast.LENGTH_SHORT).show();
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                uriList.add(uri);
                richEditer.insertImage(String.valueOf(uri));
                Toast.makeText(Post.this, String.valueOf(uri), Toast.LENGTH_LONG).show();
            }
        }
    }
    public void onClickShowQMUIDialog(View view) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(Post.this);
        builder.setTitle("标签")
                .setPlaceholder("添加你的标签")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            list_qian.add(text.toString());
                            if(list_qian.size()!=0) {
                                niceSpinner.attachDataSource(list_qian);
                             /* for(int i=0;i<list.size();i++){
                                   niceSpinner.attachDataSource(list);
                              }*/
                                Toast.makeText(getApplicationContext(), "您的标签: " + list_qian.get(0), Toast.LENGTH_SHORT).show();
                            }
                            // Toast.makeText(getApplicationContext(), "您的昵称: " + text, Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getApplicationContext(), "请填入标签", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }

    public void add_Label(String sno,String title,String label,String connect,String data){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    RequestParams params = new RequestParams("http://10.138.87.49:8080/resource/resourceinsert");
                    params.addHeader("Content-Type", "application/x-www-form-urlencoded");
                    params.addBodyParameter("resourceUserID",sno);
                    params.addBodyParameter("resourceTitle",title);
                    params.addBodyParameter("resourceType",label);
                    params.addBodyParameter("resourceContent",connect);
                    params.addBodyParameter("resourceData",data);
                    x.http().post(params, new Callback.CacheCallback<String>() {

                        @Override
                        public void onSuccess(String result) {
                            if(result.equals("success")){
                                Toast.makeText(Post.this,"帖子资源成功",Toast.LENGTH_SHORT).show();
                                Log.i("dsada",result);
                            }
                            else {
                                Toast.makeText(Post.this,"帖子发布失败",Toast.LENGTH_SHORT).show();
                                Log.i("dsada", result);
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Toast.makeText(Post.this,"网络请求错误",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(String result) {
                            return false;
                        }
                    });
                }
            }.start();
    }
}
